package pl.myapp.webtest.config.security.auth;

import org.jboss.logging.Logger;
import org.jboss.seam.security.*;
import org.jboss.seam.solder.logging.TypedCategory;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: kaafaar
 * Date: 19.06.11
 * Time: 23:34
 * To change this template use File | Settings | File Templates.
 */
public class MyAuth extends BaseAuthenticator implements Authenticator, Serializable {

    @Inject
    CredentialsImpl credentials;

    @Inject
    Identity identity;

    @Override
    public void authenticate() {

        System.out.println("Proba autoryzacji ! login ");
        System.out.println(credentials.getUsername());

        FacesContext facesContext = FacesContext.getCurrentInstance();

        if (credentials.getUsername()!=null && credentials.getPassword() != null
            && (credentials.getUsername().equals("test") && credentials.getPassword().equals("test"))
                || credentials.getUsername().equals("admin") && credentials.getPassword().equals("admin"))  {

            System.out.println("autoryzacja ok dla "+credentials.getUsername());
            identity.addRole("test", "USERS", "GROUP");

            // powiadomienia
            setStatus(AuthenticationStatus.SUCCESS);
            setUser(new UserImpl(credentials.getUsername()));
            facesContext.addMessage("loginok", new FacesMessage("Zalogowales sie poprawnie jako: "+credentials.getUsername()));
        }
        else {
            setStatus(AuthenticationStatus.FAILURE);
            facesContext.addMessage("loginfailed", new FacesMessage("Login i/lub hasło niepoprawne !"));
        }
    }
}